package three;
 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

public class A041 {



     //将有序数组转换为二叉搜索树
    public TreeNode sortedArrayToBST(int[] nums) {
        int n = nums.length;
        return build(nums,0,n-1);
    }

    public TreeNode build(int[] nums,int left,int right) {
        if(left>right) return null;
        int mid = (right+left)/2;
        //1.创建跟节点
        TreeNode root = new TreeNode(nums[mid]);
        //2.递归左右
        root.left = build(nums,left,mid-1);
        root.right = build(nums,mid+1,right);
        return root;
    }
}
